package org.bonorsoft.modules.system.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.system.entity.SysLog;
import org.bonorsoft.modules.system.service.ISysLogService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;

 /**
 * @Description: 系统日志
 * @Author: WilliamYe
 * @Date:   2020-12-04
 * @Version: V1.0
 */
@Slf4j
@Controller
@RequestMapping("/monitor")
public class SysLogController extends BonorController<SysLog, ISysLogService> {
    private String login_prefix = "monitor/logininfor";

    private String oper_prefix = "monitor/operlog";

	@Autowired
	private ISysLogService sysLogService;

	/**
	*首页
	*/
	@RequiresPermissions("system:log:view")
    @GetMapping(value = "/logininfor")
    public String index()
    {
     return login_prefix + "/logininfor";
    }

	 @RequiresPermissions("system:log:view")
	 @GetMapping(value = "/operlog")
	 public String operlog()
	 {
		 return oper_prefix + "/operlog";
	 }

	/**
	 * 分页列表查询
	 *
	 * @param sysLog
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "系统日志-分页列表查询")
	@RequiresPermissions("system:log:list")
	@GetMapping(value = "/list")
	@ResponseBody
	public Result<?> queryPageList(SysLog sysLog,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SysLog> queryWrapper = QueryGenerator.initQueryWrapper(sysLog, req.getParameterMap());
		Page<SysLog> page = new Page<SysLog>(pageNo, pageSize);
		IPage<SysLog> pageList = sysLogService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	 @RequiresPermissions("system:log:detail")
	 @GetMapping("/detail/{id}")
	 public String detail(@PathVariable("id") String id, ModelMap mmap)
	 {
		 mmap.put("sysLog", sysLogService.getById(id));
		 return oper_prefix + "/detail";
	 }

	/**
	 * 添加
	 *
	 * @param sysLog
	 * @return
	 */
	@AutoLog(value = "系统日志-添加")
	@RequiresPermissions("system:log:add")
	@PostMapping(value = "/add")
    @ResponseBody
	public Result<?> add(@Validated SysLog sysLog) {
		sysLogService.save(sysLog);
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param sysLog
	 * @return
	 */
	@AutoLog(value = "系统日志-编辑")
	@RequiresPermissions("system:log:edit")
	@PostMapping(value = "/edit")
	@ResponseBody
	public Result<?> edit(@Validated SysLog sysLog) {
		sysLogService.updateById(sysLog);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "系统日志-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		sysLogService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "系统日志-批量删除")
	@RequiresPermissions("system:log:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	@ResponseBody
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysLogService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	 /**
	  * 清空
	  *
	  * @return
	  */
	 @AutoLog(value = "系统日志-清空")
	 @RequiresPermissions("system:log:deleteBatch")
	 @PostMapping(value = "/clean")
	 @ResponseBody
	 public Result<?> clean(@RequestParam(name="logType") Integer logType) {
		 sysLogService.clean(logType);
		 return Result.ok("清空成功！");
	 }

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "系统日志-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SysLog sysLog = sysLogService.getById(id);
		return Result.ok(sysLog);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param sysLog
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysLog sysLog) {
      	return super.exportXls(request, sysLog, SysLog.class, "系统日志");
    }

    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, SysLog.class);
    }

}
